// pages/login/login.js
Page({
  data: {

  },
  submit(e){
    let username=e.detail.value.username
    let password=e.detail.value.password
   
    if(username.length==0){
      wx.showToast({
        title: '账号不可为空',
        icon:'error'
      })
      return
    }
    if(password.length==0){
      wx.showToast({
        title: '密码不可为空',
        icon:'error'
      })
      return
    }
    console.log(username,password)

    wx.request({
      url: 'http://127.0.0.1:8080/login',
      method:'POST',
      data:{
        name:username,
        password:password
      },
      success:res=>{
        console.log(res)
        let token = res.data.token
        let id=res.data.user_id
        wx.setStorageSync('token', token)
        wx.setStorageSync('user_id', id)
        if(res.statusCode==200){
          wx.showToast({
            title: '登录成功',
            icon:'success'
          })
          wx.switchTab({
            url: '/pages/index/index',
          })
          return
        }
        if (res.statusCode==500) {
          wx.showToast({
            title: '账号或者密码错误',
            icon:'error'
          })
        }
      }
    })
  },
  goto(){
    wx.navigateTo({
      url: '/pages/zhuce/zhuce',
    })
  }
 
})